<?php
require_once(LIB.'/class_Categories.php');
//require_once(LIB.'/class_Content.php');
if (!defined('TB_CATEGORIES')){ define('TB_CATEGORIES','categories');}
class manage_solution_controller{
    private $categories;
    private $structure;
    function __construct() {
        $categories=new categories();
        $categories->get_full_list();
        $cat_structure=$categories->open_categories_tree(0,0);
        array_shift($cat_structure);   
        $this->structure=$cat_structure;
        $this->categories=$categories;                
    }
    
    
    public function exec_index(){
       $content=$this->show_index_solution();
       return array('content'=>$content);
    }
    
    public function exec_create(){
       $content=$this->show_create_solution();
       return array('content'=>$content);
    }
    
    public function exec_view(){
		global $dbdefault;
        $sid =  GET_value('sid','');
		$qr3 = sprintf("UPDATE solutions SET view=view+1 WHERE idsolutions=%d AND sstatus=1",qs($sid));
		$qr4 = mysql_query($qr3,$dbdefault);
       $content=$this->show_view_solution();
       return array('content'=>$content);
    }
    
    public function exec_edit(){
       $content=$this->show_edit_solution();
       return array('content'=>$content);
    }
    
	public function exec_delete(){
        global $dbdefault;
        $s_id = GET_value('s_id');
        
        $query=  "update solutions set sstatus=0 where idsolutions=$s_id"; 

        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            //return array('success'=>TRUE);
            header('Location: index.php?m=manage_solution');
        }
    }
    private function show_view_solution(){
        global $dbdefault;
        $sid =  GET_value('sid','');
        //Lấy các comment thuộc
        $qry=sprintf("SELECT a.*, b.user_name FROM comments a JOIN users b ON a.`commentator`=b.`user_id`
            WHERE a.status=1 and idsolution=%d ORDER BY idcomments DESC",  qs($sid));
        $resqry = mysql_query($qry,$dbdefault);
        $posts=array();
        while($arr = mysql_fetch_assoc($resqry)){
            $posts[] = $arr;            
        }
        
        $query=sprintf("select a.*,UNIX_TIMESTAMP(a.createtime) as ctime, b.user_name, b.user_id from solutions a join users b on a.`author`=b.`user_id`
            where a.sstatus=1 and idsolutions=%d",  qs($sid));
       
        $res = mysql_query($query,$dbdefault);
        $row = mysql_fetch_array($res);  
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_solution/view/view_detail.html');
        $posts_tpl->set('row',$row);
        $posts_tpl->set('posts',$posts); 
		$posts_tpl->set('qr5',$qr5);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_solution/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
    private function show_create_solution(){
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_solution/view/create_form.html');

        $posts_tpl->set('categories',$this->categories->categories); 
        $posts_tpl->set('structure',$this->structure);  
		
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_solution/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
    
    
    private function show_index_solution(){
        global $dbdefault;
        $s =  GET_value('s','');
        $catid =  GET_value('catid','');
        
        $item_per_page = 25;        
        $page_num = GET_value('p',1);
        $limit = ($page_num-1)*$item_per_page;
        $offset = $item_per_page+1;
        
        $where="";
        if($s!=""){
            $where.=" and title like '%$s%'";
        }
        if($catid!=""){
            $where.=" and category_id=".$catid;
        }
        
        $qry="SELECT count(*) AS total FROM solutions where sstatus=1".$where; 
        $res_total=mysql_query($qry,$dbdefault) or die(mysql_error()."<br />".$qry);
        $total=mysql_result($res_total,0,'total');
        $num_pages=ceil($total/$item_per_page);
        
        $query="select a.*,UNIX_TIMESTAMP(a.createtime) as ctime, b.user_name from solutions a join users b on a.`author`=b.`user_id` 
            where a.sstatus=1".$where." order by a.idsolutions DESC LIMIT $limit, $offset";
        //echo $query;
        $result = mysql_query($query,$dbdefault);
        $posts = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $posts[]=$arr;
        }
        
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_solution/view/view_index.html');
        $posts_tpl ->set('s', $s);
        $posts_tpl ->set('posts', $posts);
		$posts_tpl ->set('catid', $catid);
        $posts_tpl->set('categories',$this->categories->categories); 
        $posts_tpl->set('structure',$this->structure);  
        $posts_tpl->set('page_num', $page_num);
        $posts_tpl->set('num_pages', $num_pages);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_solution/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
    
    
       private function show_left_menu($action=''){
       global $dbdefault;
       $catid =  GET_value('catid','0');
       $query1="SELECT a.*, b.category_name FROM (SELECT category_id, COUNT(*) AS total FROM solutions 
           WHERE sstatus=1 GROUP BY category_id) a JOIN categories b ON a.category_id=b.`category_id`"; 
       $result = mysql_query($query1,$dbdefault);
        $postsl = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $postsl[]=$arr;
        }
        
        if (empty($action)) $action =  GET_value('a','index');
        $tpl = new Template(MODULE_ROOT.'/manage_solution/view/left_menu.html');
        $tpl->set('a',$action);
        $tpl->set('postsl',$postsl);
        $tpl->set('catid',$catid);
        return $tpl->fetch();
    }
    
    private function show_edit_solution() {
		 global $dbdefault;
		 $sid =  GET_value('sid','');
		 $query= "SELECT * FROM solutions WHERE idsolutions=".$sid;
		 $res = mysql_query($query,$dbdefault);
         $row = mysql_fetch_array($res); 
		 $posts_tpl = new Template(MODULE_ROOT.'/manage_solution/view/edit_form.html');
         $posts_tpl ->set('row', $row);
         $posts_tpl->set('categories',$this->categories->categories); 
         $posts_tpl->set('structure',$this->structure);  
         $posts_tpl ->set('posts', $posts);
         $m_content=$posts_tpl->fetch();
         $menu = $this->show_left_menu();

         $tpl = new Template(MODULE_ROOT.'/manage_solution/view/layout.html');
         $tpl ->set('left_menu', $menu);
         $tpl ->set('content', $m_content);

         return $tpl ->fetch(); 
		}
        //-----------------------------------AJAX--------------------------------------------------
    public function ajax_create_solution(){
        global $dbdefault;
        $title = GET_value('title');
        $check = GET_value('check');
        $content = GET_value('content');
        $category = GET_value('category');

        $user_id=$_SESSION['user_id'];
        
        $query=  sprintf("insert into solutions (title, detail, author, is_new_update, customer_public,category_id) 
            values(%s,%s,%d,'1',%d,%d)", qs($title), qs($content), qs($user_id),  qs($check), qs($category));
        
        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
    }
    
	public function ajax_update_solution(){
        global $dbdefault;
		$s_title=Get_value('title');
        $s_detail = GET_value('content');
        $s_id = GET_value('id');
		$s_check=Get_value('check');
		$s_category=Get_value('category');
        $user_id=$_SESSION['user_id'];
        
        $query=  sprintf("update solutions set title=%s, detail=%s,  customer_public=%d,  category_id=%d where idsolutions=%d",  qs($s_title),  qs($s_detail), qs($s_check), qs($s_category),  qs($s_id));
        
        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
    }
	
	public function ajax_create_comment_solution() {
		global $dbdefault;
		$com_idsolution=Get_value('idsolution');
		$com_commentator=Get_value('commentator');
		$com_comment=Get_value('comment');
		$user_id=$_SESSION['user_id'];
		$query=sprintf("INSERT INTO comments(idsolution, commentator, comment) VALUES(%d, %d, %s)",qs($com_idsolution), qs($com_commentator), qs($com_comment));
		$res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
		
		}
}
?>
